<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>返回顶部按钮</title>
    <style>
        body{
            height: 5000px;
            background-image: linear-gradient(to bottom ,red,green,blue);
        }
        #an{
            position: fixed;
            right: 50px;
            top: 50%;
            margin-top: -25px;
            width: 50px;
            height: 50px;
            background-color: skyblue;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div id="an">返回顶部</div>
    <script>
        var an=document.getElementById('an');
        var t;
        an.onclick=function(){
            var i=document.documentElement.scrollTop;
            clearInterval(t);
            t=setInterval(function(){
                i-=100;
                document.documentElement.scrollTop=i;
                if(i<=0){
                    clearTimeout(t);
                }
           },20);
        }
  
    </script>
</body>
</html>